#!/bin/bash

#author: 	zhouhui
#datetime:	2022.06.08

########################
# linux shell color control
# cl_: color_
# r: red; 
# g: green; 
# b: blue 
# y: yellow; 
# p: purple
# c: close color control
# echo -e "${cl_r}echo red color${cl_c}"

cl_r="\\033[31m"
cl_g="\\033[32m"
cl_y="\\033[33m"
cl_b="\\033[34m"
cl_p="\\033[35m"
cl_c="\\033[0m"

enable_dbg=1

pinf(){
    echo -e "${cl_p}$*${cl_c}"
}
pwar(){
    echo -e "${cl_y}$*${cl_c}"
}
pmsg(){
    echo -e "${cl_b}$*${cl_c}"
}
perr(){
    echo -e "${cl_r}$*${cl_c}"
}
pdbg(){
    [ "1" = "$enable_dbg" ] && echo -e "${cl_b}$*${cl_c}"
}
#######################

#和配置工程手工保持一致: build/mk/src_scan.conf
PROJECT_LIBNAME="tcpip"
PROJECT_APPTEST="apptest"
PROJECT_APPTEST_DEBUG="apptest_d"
#workspace
INSTALL_PREFIX_PATH="/opt/$PROJECT_LIBNAME"
VERSION="1.0.0.0"
SYS_ARCH=$(uname -m)
OS_X86="x86_64"
OS_AARCH="aarch64"
CUR_PATH=$(pwd)
TOOLS_DIR="${BASH_SOURCE[0]}"
while [ -h "$TOOLS_DIR" ]; do
    # resolve $TOOLS_DIR until the file is no longer a symlink
    ROOTDIR="$( cd -P "$( dirname "$TOOLS_DIR" )" >/dev/null && pwd )"
    TOOLS_DIR="$(readlink "$TOOLS_DIR")"
    [[ $TOOLS_DIR != /* ]] && TOOLS_DIR="$ROOTDIR/$TOOLS_DIR" 
done
TOOLS_DIR="$( cd -P "$( dirname "$TOOLS_DIR" )" >/dev/null && pwd )"
ROOT_DIR=${TOOLS_DIR%/*}
ROOT_DIR=${ROOT_DIR%/*}
BUILD_DIR=${ROOT_DIR}/build/cmake
OUTBINS_DIR=${ROOT_DIR}/outbins
TEST_DIR=${ROOT_DIR}/test

#pdbg "$ROOT_DIR | $BUILD_DIR | $TEST_DIR | $OUTPUT_DIR"
###########################
function build_all()
{
    pinf "=========> [make build start] ......"
    if [ -d $LINUX_BUILD_DIR ]; then
        pinf "=== build found exist linux build directory. remove it!!! "
	rm -rf $LINUX_BUILD_DIR
    fi

    clear_all
    TOOLS_BUILD_MODE=$1   
    TOOLS_BUILD_VERSION=$2

    if [[ ! -n $TOOLS_BUILD_MODE ]];then
        TOOLS_BUILD_MODE=release
    fi

    #build configure 
    cd ${BUILD_DIR}
    current_dir=$(pwd)
    pinf "=== build directory: $(pwd) mode=${TOOLS_BUILD_MODE},vern=${TOOLS_BUILD_VERSION}"
    sh build.sh ${TOOLS_BUILD_MODE} ${TOOLS_BUILD_VERSION}
    pmsg "========= [make build end] ......"
    return 0
}



function build_main()
{
    starttime=`date +'%Y-%m-%d %H:%M:%S'`
    start_seconds=$(date --date="$starttime" +%s);
    pinf "========= CPU   arch: ${SYS_ARCH}"
    pinf "========= build path: ${CUR_PATH}"
    pmsg "========= [build start] ......          Start Time: [ ${starttime} ]"
    if [[ ${SYS_ARCH} == ${OS_X86} ]] || [[ ${SYS_ARCH} == ${OS_AARCH} ]]; then
	    build_all $1 $2 $3 $4 $5 
    else
        perr "========= [build failed!!] ...... system cpu architecture only support [${OS_X86}] or [${OS_AARCH}]" 
	    exit -1
    fi
    endtime=`date +'%Y-%m-%d %H:%M:%S'`
    end_seconds=$(date --date="$endtime" +%s);
    pmsg "========= [build start] ......          End   Time: [ ${endtime} ]"
    pmsg "========= [build successful ] ......    Build Time: [ $((end_seconds-start_seconds)) s ]"
}


function run_test()
{       
    pmsg "========= [run apptest] ......"
    sleep_sec 1
    cd $OUTBINS_DIR
    pmsg "========= run apptest: $OUTBINS_DIR ./$PROJECT_APPTEST -dir $TEST_DIR/"
	#如果apptest存在，执行apptest，否则执行apptest_d
	if [ -f $PROJECT_APPTEST ]; then
		./$PROJECT_APPTEST -dir $TEST_DIR/
		else
		if [ -f $PROJECT_APPTEST_DEBUG ]; then
			./$PROJECT_APPTEST_DEBUG -dir $TEST_DIR/
			else
			pmsg "========= app test not exist!!!"
		fi
	fi
    #拷贝报告到目标目录
    if [[ -z $1 ]];then
        pmsg "========= test_report output dir: $OUTPUT_DIR"
    else
        cp ./test_report.xml $1 
    fi
    cd -
    pmsg "========= [run app test success] ......"
}

function sleep_sec()
{
    sleep $1
}

function file_upload()
{
	pmsg "========= [file_upload] ......"
    
}


function clear_all()
{
    pmsg "========= [clear start] ......"
    cd ${OUTBINS_DIR}
    rm -rf ${OUTBINS_DIR}/*
    pmsg "========= rm outbins objs success."
    touch ${OUTBINS_DIR}/readme.txt   
    pmsg "========= [clean success] ......"
}

function build_make()
{
    starttime=`date +'%Y-%m-%d %H:%M:%S'`
    start_seconds=$(date --date="$starttime" +%s);
    pmsg "========= [build make] ......          Start Time: [ ${starttime} ]"
    if [[ ${SYS_ARCH} == ${OS_X86} ]] || [[ ${SYS_ARCH} == ${OS_AARCH} ]]; then
        cd ${OUTBINS_DIR}
        rm -rf apptest appmain
        cd ${OUTBINS_DIR}/cmaketmps
        mv ${OUTBINS_DIR}/cmaketmps/*.cpp ${OUTBINS_DIR}
        make
        mv ${OUTBINS_DIR}/*.cpp ${OUTBINS_DIR}/cmaketmps/
    else
        perr "========= [build failed!!] ...... system cpu architecture only support [${OS_X86}] or [${OS_AARCH}]"
            exit -1
    fi
    endtime=`date +'%Y-%m-%d %H:%M:%S'`
    end_seconds=$(date --date="$endtime" +%s);
    pmsg "========= [build make] ......          End   Time: [ ${endtime} ]"
    pmsg "========= [build make successful ] ......    Build Time: [ $((end_seconds-start_seconds)) s ]"
    
}

function main()
{
    cmd_arg=$1
    if [[ -z $1 ]] ; then 
    	pmsg "==usage==:./tools.sh build|make|test|clean|upload"
    	pmsg " example :./tools.sh build <release|debug> <version>"
    fi

    case ${cmd_arg} in 
    	build|b)
        build_main $2 $3 $4 $5 $6
        ;;
        test|s)
        run_test $2 $3 $4 $5 $6
        ;;
        upload|s)
        file_upload $2 $3 $4 $5 $6
        ;;
        clean|s)
        clear_all $2 $3 $4 $5 $6
        ;;
        make|s)
        build_make $2 $3 $4 $5 $6
        ;;
    esac
}


main $1 $2 $3 $4 $5 $6








